package com.bdqn.dao;

import com.bdqn.pojo.Student;

import java.util.List;

/**
 * StudentDaoImpl
 *
 * @author LILIBO
 * @since 2021-11-24
 */
public class StudentDaoImpl extends BaseDao implements StudentDao {

    /**
     * 添加学生信息
     *
     * @param student
     * @return
     */
    @Override
    public int add(Student student) {
        String sql = "insert student values(default,?,?,?,?)";
        return super.executeUpdate(sql, student.getName(), student.getSex(), student.getAddress(), student.getBirthday());
    }

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    @Override
    public int update(Student student) {
        String sql = "update student set name=?, sex=?, address=?, birthday=? where id=?";
        return super.executeUpdate(sql, student.getName(), student.getSex(), student.getAddress(), student.getBirthday(), student.getId());
    }

    /**
     * 删除学生信息
     *
     * @param id
     * @return
     */
    @Override
    public int delete(int id) {
        String sql = "delete from student where id=?";
        return super.executeUpdate(sql, id);
    }

    /**
     * 根据ID获得学生信息
     *
     * @param id
     * @return
     */
    @Override
    public Student getById(int id) {
        String sql = "select * from student where id=?";
        return super.executeQuery(Student.class, sql, id).get(0);
    }

    /**
     * 根据查询信息获得学生列表
     *
     * @param query 查询信息
     * @return
     */
    @Override
    public List<Student> getList(Student query) {
        String sql = "select * from student where 1=1";
        Object[] params = new Object[3];
        if (query != null) {
            int i = 0;
            if (query.getName() != null) {
                sql += " and `name` like CONCAT('%',?,'%')";
                params[i++] = query.getName();
            }
            if (query.getSex() != null) {
                sql += " and sex=?";
                params[i++] = query.getSex();
            }
            if (query.getAddress() != null) {
                sql += " and address like CONCAT('%',?,'%')";
                params[i++] = query.getAddress();
            }
        }
        return super.executeQuery(Student.class, sql, params);
    }
}
